/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.hibernate.model.dao;

import hibernate.pojo.Khachhang;
import hibernate.util.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
public class AdminKhachHangDAO 
{
    //<editor-fold defaultstate="collapsed" desc="1. Lấy danh sách khách hàng">
    public static List<Khachhang> AdminLayDanhSachKhachHang()
    {
        List<Khachhang> lstKhachHang = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            String hql = "from Khachhang";
            Query query = session.createQuery(hql);
            lstKhachHang = query.list();
            //session.close();
        }
        catch(HibernateException hex)
        {
            System.err.print(hex);
        }finally
        {
            session.close();
        }
        return lstKhachHang;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2. Lấy KhachHang by ID">
    public static Khachhang AdminLayKhachHangByID(int maKH)
    {
        Khachhang khachhang = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            khachhang = (Khachhang) session.get(Khachhang.class, maKH);
        }
        catch(HibernateException hex)
        {
            System.err.print(hex);
        }finally
        {
            session.close();
        }
        return khachhang;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="3. Cập nhật khách hàng">
     public static boolean AdminCapNhatKhachHang(Khachhang khachhang)
     {
         boolean flag = false;
         Session session = HibernateUtil.getSessionFactory().openSession();
         Transaction tran = null;
         try
         {
             tran = session.beginTransaction();
             session.update(khachhang);
             tran.commit();
             flag = true;
         }catch(Exception hex){
             System.err.print(hex);
             tran.rollback();
             flag = false;
         }
         finally 
         {
             session.close();
         }
         return flag;
     }
     //</editor-fold>
}